package com.student.controller;

import com.student.pojo.Student;
import com.student.service.ClassesService;
import com.student.service.KeService;
import com.student.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * @author 是一个鸽子啊
 * @date 2020/4/14
 * @qq 364826415
 */
@Controller
public class StudentController {
    @Autowired
    @Qualifier("StudentServiceImpl")
    private StudentService studentService;
    @Autowired
    @Qualifier("classesServiceImpl")
    private ClassesService classesService;
    @RequestMapping("/queryStudentList")
    private String queryStudentList(HttpServletRequest request){
        List<Student> studentList = studentService.queryStudent();
        for (Student student : studentList) {
            student.setClasses(classesService.queryClassesByID(student.getStudentCID()));
        }
        request.setAttribute("studentList",studentList);
        System.out.println(studentList);
        return "/student-table";
    }
    @RequestMapping("/updateStudent")
    private String updateStudent(Student student,HttpServletRequest request){
        studentService.updateStudent(student);
        List<Student> studentList = studentService.queryStudent();
        for (Student student1 : studentList) {
            student1.setClasses(classesService.queryClassesByID(student1.getStudentCID()));
        }
        request.setAttribute("studentList",studentList);
        return "/student-table";
    }
    @RequestMapping("/deleteStudentByID")
    private String deleteStudentByID(String studentID,HttpServletRequest request){
        studentService.delStudentByID(Integer.parseInt(studentID));
        List<Student> studentList = studentService.queryStudent();
        for (Student student1 : studentList) {
            student1.setClasses(classesService.queryClassesByID(student1.getStudentCID()));
        }
        request.setAttribute("studentList",studentList);
        return "/student-table";
    }
    @RequestMapping("/querStudentByNameA")
    private String querStudentByNameA(String studentName,String studentAddress,HttpServletRequest request){
        Map map=new HashMap();
        if (studentName!=null){
            map.put("studentName",studentName);
        }
        if (studentAddress!=null){
            map.put("studentAddress",studentAddress);
        }
        List<Student> studentList = studentService.querStudentByNameA(map);
        for (Student student1 : studentList) {
            student1.setClasses(classesService.queryClassesByID(student1.getStudentCID()));
        }
        request.setAttribute("studentList",studentList);
        return "/student-table";

    }
    @RequestMapping("/addStudent")
    private String addStudent(Student student,HttpServletRequest request){
        int i = studentService.addStudent(student);
        if (i>0){
            request.setAttribute("msg","添加成功");
        }else {
            request.setAttribute("msg","添加失败");
        }
        return "/student-add";
    }

}
